package com.kaangdev.fingerlocker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class SettingActivity extends Activity {
	EditText edtScanCount;
	EditText edtSpecialNumber;
	ImageView imgSoundOff;
	ImageView imgVibration;
	EditText edtName;
	ImageView imgLockScreen;
	SharedPreferences mPref;
	SeekBar seekBar;
	ImageView imgLaunch;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		mPref = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		edtScanCount = (EditText) findViewById(R.id.imageview1);
		edtScanCount.setText(mPref.getInt(Constant.SCAN_COUNT, 3) + "");
		edtScanCount.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog localDialog = new Dialog(SettingActivity.this);
				localDialog.setCancelable(true);
				ViewGroup view = (ViewGroup) ((LayoutInflater) SettingActivity.this
						.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(
						R.layout.dialog_seekbar, null);
				localDialog.setContentView(view);
				localDialog.setTitle("Set Can Count");
				localDialog.show();
				final TextView textView = (TextView) view
						.findViewById(R.id.txtCur);
				final SeekBar seekBar = (SeekBar) view
						.findViewById(R.id.seekBar);
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCanel = (Button) view.findViewById(R.id.btnCancel);
				seekBar.setProgress(mPref.getInt(Constant.SCAN_COUNT, 3));
				textView.setText(mPref.getInt(Constant.SCAN_COUNT, 3) + "");
				seekBar.setMax(8);
				seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						seekBar.setProgress(mPref
								.getInt(Constant.SCAN_COUNT, 3));
					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						textView.setText(progress + "");
					}
				});
				btnOk.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPref.edit()
								.putInt(Constant.SCAN_COUNT,
										seekBar.getProgress()).commit();
						edtScanCount.setText(mPref.getInt(Constant.SCAN_COUNT,
								3) + "");
						localDialog.cancel();
					}
				});
				btnCanel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						localDialog.cancel();
					}
				});
			}
		});

		imgSoundOff = (ImageView) findViewById(R.id.imageview6);
		if (mPref.getBoolean(Constant.ENABLE_SOUND, true)) {
			imgSoundOff.setImageDrawable(getResources().getDrawable(
					R.drawable.button_sound_on));
		} else {
			imgSoundOff.setImageDrawable(getResources().getDrawable(
					R.drawable.button_sound_off));
		}
		imgSoundOff.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean sound_enable = false;
				sound_enable = mPref.getBoolean(Constant.ENABLE_SOUND, true);
				mPref.edit().putBoolean(Constant.ENABLE_SOUND, !sound_enable)
						.commit();
				if (mPref.getBoolean(Constant.ENABLE_SOUND, true)) {
					imgSoundOff.setImageDrawable(getResources().getDrawable(
							R.drawable.button_sound_on));
				} else {
					imgSoundOff.setImageDrawable(getResources().getDrawable(
							R.drawable.button_sound_off));
				}
			}
		});
		imgVibration = (ImageView) findViewById(R.id.imageview4);
		if (mPref.getBoolean(Constant.ENABLE_VIBRATION, true)) {
			imgVibration.setImageDrawable(getResources().getDrawable(
					R.drawable.button_vibration_on));
		} else {
			imgVibration.setImageDrawable(getResources().getDrawable(
					R.drawable.button_vibration_off));
		}
		imgVibration.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean vibration_enable = false;
				vibration_enable = mPref.getBoolean(Constant.ENABLE_VIBRATION,
						true);
				mPref.edit()
						.putBoolean(Constant.ENABLE_VIBRATION,
								!vibration_enable).commit();
				if (mPref.getBoolean(Constant.ENABLE_VIBRATION, true)) {
					imgVibration.setImageDrawable(getResources().getDrawable(
							R.drawable.button_vibration_on));
				} else {
					imgVibration.setImageDrawable(getResources().getDrawable(
							R.drawable.button_vibration_off));
				}
			}
		});
		edtName = (EditText) findViewById(R.id.edname);
		edtName.setText(mPref.getString(Constant.NAME_USER, "Your Name"));
		edtName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog localDialog = new Dialog(SettingActivity.this);
				localDialog.setCancelable(true);
				ViewGroup view = (ViewGroup) ((LayoutInflater) SettingActivity.this
						.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(
						R.layout.dialog_input, null);
				localDialog.setContentView(view);
				localDialog.setTitle("Input your name");
				localDialog.show();
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCanel = (Button) view.findViewById(R.id.btnCancel);
				final EditText edtInput = (EditText) view
						.findViewById(R.id.edtInput);
				btnOk.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPref.edit()
								.putString(Constant.NAME_USER,
										edtInput.getText().toString()).commit();
						edtName.setText(mPref.getString(Constant.NAME_USER,
								"Your Name"));
						localDialog.cancel();
					}
				});
				btnCanel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						localDialog.cancel();
					}
				});
			}
		});
		edtSpecialNumber = (EditText) findViewById(R.id.edfile);
		edtSpecialNumber.setText(mPref.getString(Constant.SPECIAL_NUMBER,
				"Serect Code"));
		edtSpecialNumber.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog localDialog = new Dialog(SettingActivity.this);
				localDialog.setCancelable(true);
				ViewGroup view = (ViewGroup) ((LayoutInflater) SettingActivity.this
						.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(
						R.layout.dialog_input, null);
				localDialog.setContentView(view);
				localDialog.setTitle("Input your serect code");
				localDialog.show();
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCanel = (Button) view.findViewById(R.id.btnCancel);
				final EditText edtInput = (EditText) view
						.findViewById(R.id.edtInput);
				btnOk.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPref.edit()
								.putString(Constant.SPECIAL_NUMBER,
										edtInput.getText().toString()).commit();
						edtSpecialNumber.setText(mPref.getString(
								Constant.SPECIAL_NUMBER, "Serect Code"));
						localDialog.cancel();
					}
				});
				btnCanel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						localDialog.cancel();
					}
				});
			}
		});
		imgLockScreen = (ImageView) findViewById(R.id.imageview5);
		if (mPref.getBoolean(Constant.ENABLE_CLOCK, true)) {
			imgLockScreen.setImageDrawable(getResources().getDrawable(
					R.drawable.button_lock_screen_enable));
		} else {
			imgLockScreen.setImageDrawable(getResources().getDrawable(
					R.drawable.button_lock_screen_disable));
		}
		imgLockScreen.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean clock_enable = false;
				clock_enable = !mPref.getBoolean(Constant.ENABLE_CLOCK, true);
				mPref.edit().putBoolean(Constant.ENABLE_CLOCK, clock_enable)
						.commit();
				if (mPref.getBoolean(Constant.ENABLE_CLOCK, true)) {
					imgLockScreen.setImageDrawable(getResources().getDrawable(
							R.drawable.button_lock_screen_enable));
				} else {
					imgLockScreen.setImageDrawable(getResources().getDrawable(
							R.drawable.button_lock_screen_disable));
				}
			}
		});
		imgLaunch = (ImageView) findViewById(R.id.imageview7);
		imgLaunch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		});

	}
}
